package com.lin.sqltool.controller;

import com.lin.sqltool.model.ConfigGroupModel;
import com.lin.sqltool.model.ConfigItemModel;
import com.lin.sqltool.model.ConnectionInfoModel;
import com.lin.sqltool.utils.ConfigUtil;
import com.lin.sqltool.utils.ConnectionUtil;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.HashMap;
import java.util.Map;

import static com.lin.sqltool.utils.CommonUtil.showAlert;

public class ConfigDialogController {

    @FXML
    public VBox configGroupBox;

    @FXML
    public VBox configItemBox;


    public void initialize() {
        initConfig();
    }


    @FXML
    private void handleOk() {

    }

    @FXML
    private void handleCancel() {
        ((Stage) configGroupBox.getScene().getWindow()).close();
    }

    private void initConfig() {
        ConfigUtil.getConfigGroup().forEach(this::createGroupNode);
    }

    private void createGroupNode(ConfigGroupModel group) {
        Label groupLabel = new Label(group.getName());
        groupLabel.setStyle(" -fx-padding: 8px;");
        groupLabel.setPrefWidth(140);
        VBox groupBox = new VBox(groupLabel);

        groupBox.setOnMouseClicked(e -> { //设置点击样式
            configGroupBox.getChildren().forEach(child -> {
                child.setStyle("-fx-background-color: #f4f4f4;");
            });
            groupBox.setStyle("-fx-background-color: #9dcbfb;");
            configItemBox.getChildren().clear();
            group.getItems().forEach(       //设置内容
                    item -> {
                        configItemBox.getChildren().add(createControlByType(item));
                    }
            );
        });

        configGroupBox.getChildren().add(groupBox);
    }


    private HBox createControlByType(ConfigItemModel item) {
        HBox control = new HBox(5);
        control.setSpacing(10);
        control.setPadding(new Insets(14, 20, 0, 14));
        Label itemLabel = new Label(item.getName() + " : ");
        itemLabel.setMinWidth(60);
        itemLabel.setPrefHeight(22);

        switch (item.getType()) {
            case "select":
                ComboBox<String> comboBox = new ComboBox<>(FXCollections.observableList(item.getOptions()));
                comboBox.setValue(item.getValue());
                comboBox.setItems(FXCollections.observableList(item.getOptions()));
                comboBox.setMinWidth(140);
                comboBox.setPrefHeight(22);
                control.getChildren().addAll(itemLabel , comboBox);
                break;
            case "checkbox":
                CheckBox checkBox = new CheckBox();
                checkBox.setPrefHeight(22);
                control.getChildren().addAll(itemLabel , checkBox);
                break;
            case "text":
                TextField textField = new TextField();
                textField.setMinWidth(140);
                textField.setPrefHeight(22);
                control.getChildren().addAll(itemLabel , textField);

                break;
        }
        return control;
    }


}
